package com.gouda.core.service.impl.useCity;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.gouda.core.dao.UseCityDao;
import com.gouda.core.dao.UseDistrictDao;
import com.gouda.core.dto.useCity.UseDistrictDto;
import com.gouda.core.entity.UseCityEntity;
import com.gouda.core.entity.UseDistrictEntity;
import com.gouda.core.enums.GeneralStateEnum;
import com.gouda.core.service.uesCity.UseDistrictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by souu on 15/12/5.
 */
@Service
public class UseDistrictServiceImpl implements UseDistrictService {

    @Autowired
    private UseDistrictDao useDistrictDao;

    @Autowired
    private UseCityDao useCityDao;

    @Override
    public List<UseDistrictDto> getUseDistrictByCity(long cityId) {
        if (cityId <= 0) {
            return null;
        }

        List<UseDistrictEntity> list = this.useDistrictDao.getUseDistrictByCity(cityId);
        if (list == null || list.isEmpty()) {
            return null;
        }
        List<UseDistrictDto> result = Lists.newArrayList();
        for (UseDistrictEntity entity : list) {
            result.add(this.copyDto(entity));
        }
        return result;
    }

    @Override
    public void createUseDistrict(long cityId, String districtName, String lon, String lat) {
        if (cityId <= 0 || Strings.isNullOrEmpty(districtName)
                || Strings.isNullOrEmpty(lon) || Strings.isNullOrEmpty(lat)) {
            return;
        }
        UseCityEntity city = this.useCityDao.unique(UseCityEntity.class, cityId);
        if (city == null) {
            return;
        }

        UseDistrictEntity entity = new UseDistrictEntity();
        entity.setCityId(cityId);
        entity.setCreateTime(new Date());
        entity.setDistrictName(districtName);
        entity.setLat(lat);
        entity.setLon(lon);
        entity.setState(GeneralStateEnum.YES.getCode());
        this.useDistrictDao.insert(entity);
    }

    @Override
    public void updateUseDistrict(long id, long cityId, String districtName, String lon, String lat, GeneralStateEnum stateEnum) {
        if (id <= 0) {
            return;
        }

        if (cityId >= 0) {
            UseCityEntity city = this.useCityDao.unique(UseCityEntity.class, cityId);
            if (city == null) {
                return;
            }
        }

        UseDistrictEntity entity = this.useDistrictDao.unique(UseDistrictEntity.class, id);
        boolean isUpdate = false;
        if (cityId >= 0) {
            entity.setCityId(cityId);
            isUpdate = true;
        }
        if (!Strings.isNullOrEmpty(districtName)) {
            entity.setDistrictName(districtName);
            isUpdate = true;
        }
        if (!Strings.isNullOrEmpty(lon)) {
            entity.setLon(lon);
            isUpdate = true;
        }
        if (!Strings.isNullOrEmpty(lat)) {
            entity.setLat(lat);
            isUpdate = true;
        }
        if (stateEnum != null) {
            entity.setState(stateEnum.getCode());
            isUpdate = true;
        }
        if (isUpdate) {
            this.useDistrictDao.update(entity);
            //todo 发送修改范围消息
        }
    }

    private UseDistrictDto copyDto(UseDistrictEntity entity) {
        UseDistrictDto dto = new UseDistrictDto();
        dto.setCityId(entity.getCityId());
        dto.setDistrictName(entity.getDistrictName());
        dto.setId(entity.getId());
        dto.setLat(entity.getLat());
        dto.setLon(entity.getLon());
        return dto;
    }
}
